//
//  ZH_GoodsListTableViewCell.m
//  ChangeMerchants
//
//  Created by zhangliwen on 2018/9/10.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_GoodsListTableViewCell.h"
#import "GC_FeaturesSelectCollectionViewCell.h"
@interface ZH_GoodsListTableViewCell()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
///内容
@property(nonatomic, weak) UICollectionView *collectionView;
///数据
@property(nonatomic, strong) NSMutableArray *dataArray;
@end
@implementation ZH_GoodsListTableViewCell

///创建
+ (instancetype)cellWithTableView:(UITableView*)tableView
{
    static NSString *ID = @"ZH_GoodsListTableViewCell";
    ZH_GoodsListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[ZH_GoodsListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}
///初始化
-(void)initDefault
{
    [self unShowClickEffect];
    
}

- (void)loadSubviewConstraints{
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(FITSCALE(9));
        make.left.equalTo(self.contentView).offset(FITSCALE(11));
        make.right.equalTo(self.contentView).offset(FITSCALE(-11));
        make.bottom.equalTo(self.contentView).offset(FITSCALE(0));
    }];
}
///更新ui
- (void)updateUiForData
{
    [self.collectionView reloadData];
}
///更新 ui
- (void)updateUiForData:(NSMutableArray *)datas
{
    if(datas.count > 0){
        [self.dataArray removeAllObjects];
        [self.dataArray addObjectsFromArray:datas];
        
        NSInteger item = self.dataArray.count;
        NSInteger rowNum = (item / 4) + ((item % 4) > 0 ? 1 : 0);
        
        CGFloat collectionHeight = FITSCALE(88) * rowNum + (rowNum * FITHEIGHT(0));
        
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(collectionHeight);
        }];
        
        [self.collectionView reloadData];
    }else{
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(HEIGHT_TRANSFORMATION(0));
        }];
    }
}
//- (void)updataUI{
//    self.backView.hidden = YES;
//}

#pragma mark -- UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
        if(self.onClickBlock){
            self.onClickBlock([NSString stringWithFormat:@"%@",self.dataArray[indexPath.row][@"id"]]);
        }
    
    [self.collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGSize size = CGSizeMake((kMainScreenWidth - FITSCALE(46) - WIDTH_TRANSFORMATION(160)) / 4, FITSCALE(88));
    return size;
}

///设置行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return FITHEIGHT(0);
}

///设置列间距，配合item宽度来设置
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return WIDTH_TRANSFORMATION(0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GC_FeaturesSelectCollectionViewCell *cell = [GC_FeaturesSelectCollectionViewCell collectionView:collectionView cellForItemAtIndexPath:indexPath];
    if(self.dataArray.count > 0){
        [cell updateUiForDatas:self.dataArray[indexPath.row]];
    }
    
    return cell;
}



#pragma mark -- getter,setter

///内容
- (UICollectionView *)collectionView
{
    if(!_collectionView){
        UICollectionView *collectionView = [UICollectionView createCollectionViewWithDelegateTarget:self forScrollDirection:UICollectionViewScrollDirectionVertical];
        collectionView.layer.cornerRadius = FITSCALE(3);
        collectionView.layer.masksToBounds = YES;
        collectionView.clipsToBounds = YES;
        //        collectionView.pagingEnabled = YES;
        [self.contentView addSubview:_collectionView = collectionView];
        //注册
        [GC_FeaturesSelectCollectionViewCell registerCollectionView:collectionView];
    }
    return _collectionView;
}


///数据
- (NSMutableArray *)dataArray
{
    if(!_dataArray){
        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataArray;
}


@end
